<?php


namespace App\Services;


use App\Models\AppletsIndexListCategory;
use App\Models\MemberOther;
use App\Models\OrderRiskAlipay;
use App\Models\OtherOrderAllocate;
use Illuminate\Support\Facades\Redis;

class SafeService
{
    /**
     * 微信
     * @param $message
     * @param array $request
     * @param array $response
     */
    public static function requestLog($message,$request=[],$response=[]){
        $content = [
            'request'=>is_object($request) ? object_array($request) : $request,
            'response'=>is_object($response) ? object_array($response) : $response,
        ];
        \Illuminate\Support\Facades\Log::debug($message,$content);
    }
    public static function addLog($message,$request=[],$response=[]){
        $content = [
            'request'=>is_object($request) ? object_array($request) : $request,
            'response'=>is_object($response) ? object_array($response) : $response,
        ];
        \Illuminate\Support\Facades\Log::debug($message,$content);
    }


    public static function requestLog2($message,$request=[],$response=[]){
        $content = [
            'request'=>is_object($request) ? object_array($request) : $request,
            'response'=>is_object($response) ? object_array($response) : $response,
        ];
        \Illuminate\Support\Facades\Log::debug($message,$content);
    }
    /**
     * 获取accessToken
     * @return array
     */
    public static function getAccessToken(){
        $url = "https://digital.jiujiuzu.com/api/auth/access_token";
        $param = [
            'clientId'=>'6eeq3sv40a8ecjtfereafkcur11gw3v0',
            'secret'=>'qKehHxLY1DbUj-QVGVVeJm',
        ];
        $token = Redis::get('safe_token');
        if($token){
            return $token;
        }else{
            $result = http_get($url,$param);
            $result = json_decode($result,true);
            if (isset($result['code'])&&$result['code']==10000) {
                Redis::setex('safe_token',86400,$result['data']);
                return $result['data'];
            }else{
                return '';
            }
        }


    }


    /**
     * 获取九云风控报告
     * @param $code
     * @return array
     */
    public static function getRiskDdata($name,$cert_no,$mobile){
        $accessToken = self::getAccessToken();
        if(!$accessToken){
            $flag =  false;
        }else{
            $data =  [];
            $url = 'https://digital.jiujiuzu.com/api/risk/data/1733356138976837633';
            $param = [
                'clientId'=>'6eeq3sv40a8ecjtfereafkcur11gw3v0',
                'reqId'=>time().uniqid(),
                'data'=>['name'=>$name,'idCard'=>$cert_no,'mobile'=>$mobile],
            ];
            var_dump($param);
            $accessToken='Bearer '.$accessToken;
            $result = http_post($url,json_encode($param),['Content-type: application/json;charset=UTF-8','Authorization:'.$accessToken]);
            $result = json_decode($result,true);
            dd($result);
            self::requestLog('获取九云风控报告',$param,$result);
            //成功
            if (isset($result['errcode']) && $result['errcode'] == 0) {
                $flag =  true;
                $data = $result;
            }else{
                $flag =  false;
            }
        }
        return ['status'=>$flag, 'message'=>$flag ? '成功' : '失败','data'=>$data];
    }

    /**
     * 支付宝用户风险识别
     */
    public static function alipayRead($userId,$member_id){
        $accessToken = self::getAccessToken();
        if(!$accessToken){
            $flag =  false;
        }else{
            $data =  [];
            $url = 'https://digital.jiujiuzu.com/api/risk/data/1724722992177799170';
            $param = [
                'clientId'=>'6eeq3sv40a8ecjtfereafkcur11gw3v0',
                'reqId'=>time().uniqid(),
                'data'=>['userId'=>$userId,'currentPhase'=>'risk_T0'],
            ];
            $accessToken='Bearer '.$accessToken;
            $result = http_post($url,json_encode($param),['Content-type: application/json;charset=UTF-8','Authorization:'.$accessToken]);
            $result = json_decode($result,true);
            self::requestLog('获取九云风控报告',$param,$result);
            //成功
            if (isset($result['code']) && $result['code'] == 10000) {
                $flag =  true;
                $level=$result['data']['level'];
                $data='未知';
                if($level==998){
                    $data='极⾼危';
                }
                if($level==996){
                    $data='⾼危';
                }
                if($level==979){
                    $data='中风险';
                }
                if($level==975){
                    $data='正常';
                }
                if($level==969){
                    $data='低风险';
                }
                if($level==989){
                    $data='优质';
                }
                OtherOrderAllocate::where('member_id',$member_id)->increment('alipayRiskCount');
                $member=MemberOther::where('user_id',$userId)->first();
                OrderRiskAlipay::setRiskData($member['cert_name'],$member['cert_no'],$member['mobile'],$data,1,$result,$userId,$member_id,$param['reqId']);
            }else{
                $data='未知';
                $flag =  false;
                $member=MemberOther::where('user_id',$userId)->first();
                OrderRiskAlipay::setRiskData($member['cert_name'],$member['cert_no'],$member['mobile'],$data,2,$result,$userId,$member_id,$param['reqId']);
            }
            MemberOther::where('user_id',$userId)->update(['level'=>$data]);
            return $data;

        }
    }


}
